//
//  GroupListViewController.m
//  FLORIDA
//
//  Created by 魏延(颜魏) on 2023/8/4.
//

#import "GroupListViewController.h"
#import "BlutoothGroupModel.h"
#import "BlutoothGroupModel.h"
#import "ProgressHUD.h"

@interface GroupListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSMutableArray<BlutoothGroupModel *> *dataSource;

@end

@implementation GroupListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource = [[NSMutableArray alloc] init];
    self.tableView.separatorColor = [[UIColor blackColor] colorWithAlphaComponent:0.2];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    if (@available(iOS 15.0, *)) {
        self.tableView.sectionHeaderTopPadding = 0;
    }

    [self reloadData];
}

- (void)reloadData {
    [self.dataSource removeAllObjects];

    NSString *groupStr = [[NSUserDefaults standardUserDefaults] objectForKey:BatteryGroupBluList] ?: @"";
    if (groupStr.length) {
        NSArray *dataArray = [BlutoothGroupModel jsonCovertToModel:groupStr];
        [self.dataSource addObjectsFromArray:dataArray];
    }
    
    [self.tableView reloadData];
}

- (IBAction)actionBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    cell.contentView.backgroundColor = [UIColor clearColor];
    
    cell.textLabel.text = self.dataSource[indexPath.row].groupName;
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.font = [UIFont systemFontOfSize:18];
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *groupStr = [[NSUserDefaults standardUserDefaults] objectForKey:BatteryGroupBluList] ?: @"";
    NSArray *dataArray = [BlutoothGroupModel jsonCovertToModel:groupStr];
    for (BlutoothGroupModel *model in dataArray) {
        if ([model.groupId isEqualToString:self.dataSource[indexPath.row].groupId]) {
            NSMutableArray *arr = [NSMutableArray arrayWithArray:self.dataSource[indexPath.row].bluList];
            [arr addObject:self.battery];
            model.bluList = arr;
        }
    }
    NSString *str = [BlutoothGroupModel toJson:dataArray];
    [[NSUserDefaults standardUserDefaults] setObject:str forKey:BatteryGroupBluList];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [ProgressHUD showSuccessMessage:@"Add to group successfully"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:kBTBluGroupChanged object:nil];
        [self.navigationController popViewControllerAnimated:YES];
    });
}

@end
